﻿using System;

using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;

namespace Fuse8.DomainFramework.MongoDbWrapper
{
    public static class MongoDbWrapperController
    {
        public static void RegisterForMongoDb<TEntity>() where TEntity : class, IEntityObject<string>, new()
        {
            var wrapperType = MongoDbEntityBuilder.CreateInstance<TEntity>().GetType();

            DependencyServiceLocator.Current.SetDefaultService(
                Activator.CreateInstance(
                    Type.GetType("BaseMongoDbEntityDataRepositoryMapper`2").MakeGenericType(
                        new Type[] 
                        {   
                            typeof(TEntity), 
                            wrapperType 
                        })));

            DependencyServiceLocator.Current.SetDefaultService(
                Activator.CreateInstance(
                    Type.GetType("MongoDbEntityDataRepository`2").MakeGenericType(
                        new Type[] 
                        {   
                            typeof(TEntity), 
                            wrapperType
                        })));

            DependencyServiceLocator.Current.SetDefaultService<IUnitOfWorkFactory<TEntity>>(
                new HttpContextMongoDbUnitOfWorkFactory<TEntity>());
        }
    }
}
